package cn.kgc.tangcco.work01_10.controller;

import cn.kgc.tangcco.work01_10.pojo.Faqs;
import cn.kgc.tangcco.work01_10.service.FaqsService;
import cn.kgc.tangcco.work01_10.vo.FaqsVo;
import com.sun.org.glassfish.external.probe.provider.annotations.ProbeParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class FaqsController {

    @Resource
     private  FaqsService faqsService;

    @GetMapping("/select")
    public FaqsVo select(@RequestParam(required = false, name = "title", defaultValue = "")String title) {
        FaqsVo faqsVo=new FaqsVo();
        try {
            List<Faqs> faqs = faqsService.selects(title);
            faqsVo.setFaqs(faqs);
            faqsVo .setMsg("");
            faqsVo.setStatus(true);
        } catch (Exception e) {
            e.printStackTrace();
            faqsVo.setMsg(e.getMessage());
            faqsVo.setStatus(false);
        }
        return faqsVo;
    }

    @PostMapping(value = "/add",produces = "application/json;charset=UTF-8")
    public FaqsVo add(@RequestBody Faqs faqs){
        System.out.println("开始增加");
        FaqsVo faqsVo = new FaqsVo();
        Date date = new Date();//获取当前的日期
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String str = df.format(date);//获取String类型的时间
        faqs.setCreateDate(str);
        int add =faqsService.inserts(faqs);
        if(add>0){
            faqsVo.setMsg("添加成功");
        }else{
            faqsVo.setMsg("添加成功");
        }
        return faqsVo;
    }
    @GetMapping("/faqsById")
    public FaqsVo Byid(@RequestParam("id")Integer id){
        FaqsVo faqsVo = new FaqsVo();
        Faqs xiangqing = faqsService.xiangqing(id);
        faqsVo.setFaqss(xiangqing);
        return faqsVo;
    }



}
